
package gui.properties;

import gui.properties.groups.AbstractGroup;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.util.Scanner;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import util.reflection.MethodAction;
import util.reflection.Reflection;

import controller.AbstractController;
import controller.PropertiesController;
import controller.SLogoController;

import model.Model;

/**
 * @author Shun Fan
 * 
 */
public class ScenePropertiesPanel extends JPanel implements Observer
{
	private JButton mySetBackgroundButton;
	private JTextField myWorldWidth;
	private JTextField myWorldHeight;
	
	private Model myModel;
	private AbstractController myController;
	//private AbstractController controller;
	
	ArrayList<AbstractGroup> linkedObjects = new ArrayList<AbstractGroup>();
	private static final String SCANNER_DELIMITTER = ";\\s*";
	
	public ScenePropertiesPanel(String resourceFile, Model model, AbstractController controller)
	{
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		myModel = model;
		myController = controller;
		myModel.addObserver(myModel.WORLD_OBSERVER, this);
		createBackgroundChooser();
		
		try {
			Scanner reader = new Scanner(new File(resourceFile)).useDelimiter(SCANNER_DELIMITTER);
			while(reader.hasNext()) {
				AbstractGroup p = (AbstractGroup) Reflection.createInstance(reader.next(), myModel, myController, reader.nextLine()) ;
				linkedObjects.add(p);
				add(p);
				//System.out.print(reader.next());
				//System.out.print(" *");
				//System.out.print(reader.nextLine());
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}	
	}

	private void createBackgroundChooser() {
		JPanel panel = new JPanel();
		mySetBackgroundButton = new JButton("Choose Image");

		

			mySetBackgroundButton.addActionListener(
					new MethodAction(myController, 
					"setBackgroundImage", myModel, mySetBackgroundButton)	
			);

		
		panel.add(new JLabel("Background Image"));
		panel.add(mySetBackgroundButton);
		
		
		add(panel);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		
		
	}
	
	public static void main(String args[]) throws IOException {
		Model model = new Model();
		PropertiesController controller = new PropertiesController(model);
		ScenePropertiesPanel p = new ScenePropertiesPanel("src/resources/properties/SceneProperties.txt", model, controller);
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//f.setLayout(new FlowLayout());
		f.add(p);
		p.update(null, null);
		f.setSize(1000,500);
		f.setVisible(true);
	}
}
